package com.wd.live.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;


import com.wd.live.model.SpUtils;
import com.wd.live.model.utils.ToastFormat;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


/**
 * Description : BaseActivity
 *
 * @author 邵玺博
 * @date 2022/11/03
 */
public abstract class BaseActivity <VM extends BaseViewModel,DB extends ViewDataBinding> extends AppCompatActivity implements Observer {
    public VM viewmodel;
    public DB databind;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SpUtils.init(this);
        ToastFormat.InitToast(this);
        //设置布局
        databind= DataBindingUtil.setContentView(this,initLayout());
        databind.setLifecycleOwner(this);
        //抽取基类
        initViewModel();
        initData();
    }

    protected abstract void initData();

    private void initViewModel() {
      //判断为空赋值
        if(viewmodel==null){
            Class clazz=null;
            Type type = getClass().getGenericSuperclass();
            if(type instanceof ParameterizedType){
                clazz=(Class)((ParameterizedType)type).getActualTypeArguments()[0];
            }else{
             clazz=getClass();
            }
            viewmodel=(VM)new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(clazz);
        }
    }

    protected abstract int initLayout();

}
